package cn.com.headfree.util;

import cn.com.headfree.vo.DetailTable;
import cn.com.headfree.vo.SkillAndTechnicalVo;
import cn.com.headfree.vo.TechnicalReportVo;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.MiniTableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.List;

public class DetailTablePolicy extends DynamicTableRenderPolicy {

    // 技术革新、技术改造、科技成果转化、关键问题处理填充数据所在行数
    int skillStartRow = 4;
    // 编写操作规程、规范、标准、教案及发表论文、著作等情况填充数据所在行数
    int standStartRow = 7;
    // 参加技能竞赛获奖情况填充数据所在行数
    int contentStartRow = 10;
    // 承担技艺传授、技能培训工作情况填充数据所在行数
    int teachStartRow = 13;
    // 获得荣誉称号填充数据所在行数
    int honorStartRow = 16;

    @Override
    public void render(XWPFTable table, Object data) {
        if (null == data) return;
        DetailTable detailData = (DetailTable) data;

        // 获得荣誉称号
        List<RowRenderData> honorDatasVo = detailData.getHonorDatasVo();
        if (null != honorDatasVo) {
            table.removeRow(honorStartRow);
            // 循环插入行
            for (int i = 0; i < honorDatasVo.size(); i++) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(honorStartRow);
                for (int j = 0; j < 5; j++){
                    XWPFTableCell cell = insertNewTableRow.createCell();
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                }
                TableTools.mergeCellsHorizonal(table, honorStartRow, 1, 3);
                MiniTableRenderPolicy.Helper.renderRow(table, honorStartRow, honorDatasVo.get(i));
            }
        }
        // 承担技艺传授、技能培训工作情况
        List<RowRenderData> teachArtVo = detailData.getTeachArtVo();
        if (null != teachArtVo) {
            table.removeRow(teachStartRow);
            // 循环插入行
            for (int i = 0; i < teachArtVo.size(); i++) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(teachStartRow);
                for (int j = 0; j < 5; j++) {
                    XWPFTableCell cell = insertNewTableRow.createCell();
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                }
                TableTools.mergeCellsHorizonal(table, teachStartRow, 1, 3);
                MiniTableRenderPolicy.Helper.renderRow(table, teachStartRow, teachArtVo.get(i));
            }
        }
        // 参加技能竞赛获奖情况
        List<RowRenderData> contestInfoVo = detailData.getContestInfoVo();
        if (null != contestInfoVo) {
            table.removeRow(contentStartRow);
            // 循环插入行
            for (int i = 0; i < contestInfoVo.size(); i++) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(contentStartRow);
                for (int j = 0; j < 5; j++) {
                    XWPFTableCell cell = insertNewTableRow.createCell();
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                }
                MiniTableRenderPolicy.Helper.renderRow(table, contentStartRow, contestInfoVo.get(i));
            }
        }
        // 编写操作规程、规范、标准、教案及发表论文、著作等情况
        List<RowRenderData> standAndScienceVo = detailData.getStandAndScienceVo();
        if (null != standAndScienceVo) {
            table.removeRow(standStartRow);
            // 循环插入行
            for (int i = 0; i < standAndScienceVo.size(); i++) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(standStartRow);
                for (int j = 0; j < 5; j++) {
                    XWPFTableCell cell = insertNewTableRow.createCell();
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                }
                // 合并单元格
                TableTools.mergeCellsHorizonal(table, standStartRow, 1, 2);
                MiniTableRenderPolicy.Helper.renderRow(table, standStartRow, standAndScienceVo.get(i));
            }
        }
        // 技术革新、技术改造、科技成果转化、关键问题处理
        List<RowRenderData> skillAndTechnicalVos = detailData.getSkillAndTechnicalVo();
        if (null != skillAndTechnicalVos) {
            table.removeRow(skillStartRow);
            // 循环插入行
            for (int i = 0; i < skillAndTechnicalVos.size(); i++) {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(skillStartRow);
                for (int j = 0; j < 5; j++) {
                    XWPFTableCell cell = insertNewTableRow.createCell();
                    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
                }
                // 合并单元格
                TableTools.mergeCellsHorizonal(table, skillStartRow, 1, 2);
                MiniTableRenderPolicy.Helper.renderRow(table, skillStartRow, skillAndTechnicalVos.get(i));
            }
        }

    }
}